package com.sanxiau.dormitory.yang.controller;

import com.sanxiau.dormitory.yang.entity.Attendance;
import com.sanxiau.dormitory.yang.entity.Dorout;
import com.sanxiau.dormitory.yang.service.DoroutService;
import com.sanxiau.dormitory.yang.vo.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/dorout")
public class DoroutController {
    @Autowired
    DoroutService doroutService;

    @ResponseBody
    @RequestMapping("/setCheckTime")
    public ResultInfo setCheckTime(String dates, String beginTime, String endTime,HttpSession session){


        String[] strings = dates.split(",");
        List<Date> dateList = new ArrayList<>();
        for (String s : strings) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                dateList.add(sdf.parse(s));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        //System.out.println(dateList);
        ResultInfo resultInfo = doroutService.setCheckTime(dateList, beginTime, endTime, session);
        return resultInfo;
    }
    @ResponseBody
    @RequestMapping("/isDoroutExist")
    public ResultInfo isDoroutExist(String dates, String beginTime, String endTime, HttpSession session){
        String [] strings =  dates.split(",");
        List<Date> dateList = new ArrayList<>();
        for (String s : strings) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                dateList.add(sdf.parse(s));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        //System.out.println(dateList);
        ResultInfo resultInfo = doroutService.isDoroutExist(dateList, beginTime, endTime, session);
        return resultInfo;
    }
    @RequestMapping("/doroutRecord")
    public String doroutRecord(Model model, HttpSession session){
        List<Dorout> dorouts = doroutService.allDorout();
        model.addAttribute("dorouts",dorouts);
        return "yang/doroutRecord";
    }

    @RequestMapping("setDorout")
    public String setDorout(){
        return "yang/setDorout";
    }

}
